package com.cloudeasy.area.controller;

import com.cloudeasy.api.util.ResultUtil;
import com.cloudeasy.api.vo.area.AreaAttentionVO;
import com.cloudeasy.api.vo.area.AreaVO;
import com.cloudeasy.api.vo.PageResult;
import com.cloudeasy.api.vo.Result;
import com.cloudeasy.api.vo.user.UserInfoVO;
import com.cloudeasy.area.client.UserClient;
import com.cloudeasy.area.po.AreaAttention;
import com.cloudeasy.area.service.AreaAttentionService;
import com.cloudeasy.area.service.AreaService;
import com.cloudeasy.area.util.AreaAttentionUtil;
import com.cloudeasy.area.util.AreaUtil;
import com.cloudeasy.core.bean.JwtParser;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
@RequestMapping("/attention")
public class AreaAttentionController {

    private final AreaService areaService;

    private final AreaAttentionService areaAttentionService;

    private final UserClient userClient;

    private final JwtParser jwtParser;

    public AreaAttentionController(AreaService areaService, AreaAttentionService areaAttentionService, UserClient userClient, JwtParser jwtParser) {
        this.areaService = areaService;
        this.areaAttentionService = areaAttentionService;
        this.userClient = userClient;
        this.jwtParser = jwtParser;
    }

    /**
     * 添加关注
     *
     * @param authentication {@link Authentication}
     * @param areaId         区域Id
     * @return {@link Result<AreaAttentionVO>}
     */
    @PostMapping("/{areaId}")
    public Result<AreaAttentionVO> attention(Authentication authentication, @PathVariable("areaId") String areaId) {
        String userId = (String) jwtParser.getOnBody(authentication, "userId");
        return ResultUtil.success(AreaAttentionUtil.convert(areaAttentionService.attention(userId, areaId)));
    }

    /**
     * 移除关注
     *
     * @param authentication {@link Authentication}
     * @param areaId         区域Id
     * @return {@link Result}
     */
    @DeleteMapping("/{areaId}")
    public Result removeAttention(Authentication authentication, @PathVariable("areaId") String areaId) {
        String userId = (String) jwtParser.getOnBody(authentication, "userId");
        areaAttentionService.removeAttention(userId, areaId);
        return ResultUtil.success();
    }

    /**
     * 获取登录用户的关注信息
     *
     * @param authentication {@link Authentication}
     * @return {@link List<AreaVO>}的{@link Result}
     */
    @GetMapping
    public Result<List<AreaVO>> myAttention(Authentication authentication) {
        String userId = (String) jwtParser.getOnBody(authentication, "userId");
        List<String> areaIds = areaAttentionService.listByAttentionId(userId).stream().map(AreaAttention::getAreaId).collect(Collectors.toList());
        return ResultUtil.success(AreaUtil.convert(areaService.listByIds(areaIds)));
    }

    /**
     * 获取关注区域的用户信息
     *
     * @param areaId 区域Id
     * @return {@link List<UserInfoVO>}的{@link Result}
     */
    @GetMapping("/{areaId}")
    public Result<List<UserInfoVO>> attentionList(@PathVariable("areaId") String areaId) {
        List<String> userIds = areaAttentionService.list(areaId).stream().map(AreaAttention::getUserId).collect(Collectors.toList());
        if (userIds.size()==0){
            return ResultUtil.success();
        }
        return userClient.list(userIds, null, null, null);
    }

    /**
     * 分页获取关注区域的用户信息
     *
     * @param areaId 区域Id
     * @param page   页数
     * @param size   每页大小
     * @return {@link List<UserInfoVO>}的{@link Result}
     */
    @GetMapping("/{areaId}/{page}/{size}")
    public Result<PageResult<UserInfoVO>> attentionPage(@PathVariable("areaId") String areaId, @PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        List<String> userIds = areaAttentionService.page(areaId, page, size).stream().map(AreaAttention::getUserId).collect(Collectors.toList());
        if (userIds.size()==0){
            return ResultUtil.success();
        }
        return userClient.page(userIds, null, null, null, page, size);
    }
}
